package system.fan.controller;


import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import system.fan.bean.AjaxMessage;
import system.fan.pojo.Customer;
import system.fan.bean.TableData;
import system.fan.service.CustomerService;

@RestController
@RequestMapping("/sys/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @PostMapping("/add")
    public AjaxMessage add(Customer customer){
        try {
            customerService.insertCustomer(customer);
            return  new AjaxMessage(true,"添加成功");
        } catch (Exception e) {
            return  new AjaxMessage(false,"添加失败");
        }
    }
    @GetMapping("/table")
    public TableData<Customer> select(@RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "2") Integer limit){
        PageInfo<Customer> customerPageInfo = customerService.selectAllByPage(page, limit);
        TableData<Customer> tableData=new TableData<Customer>(0,
                "success",
                customerPageInfo.getTotal(),
                customerPageInfo.getList());
//        按照要求,返回数据格式!
        return tableData;
    }
    @PostMapping("/update")
    public AjaxMessage update(Customer customer){
        try {
            customerService.updateCustomer(customer);
            return  new AjaxMessage(true,"更新成功");
        } catch (Exception e) {
            return  new AjaxMessage(false,"更新失败");
        }
    }
    @PostMapping("/delete")
    public AjaxMessage delete(int[] ids){
        if (ids!= null && ids.length>0){
            customerService.deleteCustomerByIDs(ids);
            return  new AjaxMessage(true,"删除成功");
        }
        else {
            return  new AjaxMessage(false,"删除失败");
        }
    }
}
